package com.kupdater.util;

import com.sun.org.apache.bcel.internal.generic.*;
import com.sun.org.apache.bcel.internal.classfile.Method;

import java.util.ArrayList;

/**
 * Created by IntelliJ IDEA.
 * User: Jan Ove
 * Date: 30.jan.2009
 * Time: 19:06:02
 * To change this template use File | Settings | File Templates.
 */
public class RSVersion {
    public static void getRSVersion(ClassGen[] classes){
        for(ClassGen cG: classes){
            if(cG.getClassName().equals("client")){
                 // Get the version:
                ArrayList<Integer> possibleVersions = new ArrayList();
                for (Method m: cG.getMethods()) {
                    if (m.getName().equals("main")) {
                        //Template class for building/editing a method inside the class
                        MethodGen methodGen = new MethodGen(m, "main", cG.getConstantPool());
                        //Template class acting as a container for a list of Instruction objects
                        InstructionList iList = methodGen.getInstructionList();
                        //Abstract super class for all Java byte codes.
                        Instruction[] instructions = iList.getInstructions();
                        for (Instruction i: instructions) {
                            //SIPUSH -> Bytecode of short being pushed on stack
                            if (i instanceof SIPUSH) {
                                //Adding the int value of the short value to the arraylist of possible version values
                                possibleVersions.add(((SIPUSH)i).getValue().intValue());
                            }
                        }
                    }
                }
                for(int possible: possibleVersions){
                    if(!(possible == 1024 || possible == 768 || possible == -11014))
                        System.out.println("RS version: " + possible + "\n");
                }
            }
        }

    }
}
